package cn.edu.sgu.www.mhxysy.controller.prop;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.prop.PropDTO;
import cn.edu.sgu.www.mhxysy.entity.prop.Prop;
import cn.edu.sgu.www.mhxysy.pager.prop.PropPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.prop.PropService;
import cn.edu.sgu.www.mhxysy.vo.prop.PropVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "道具管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/prop", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class PropController {

    private final PropService propService;

    @Autowired
    public PropController(PropService propService) {
        this.propService = propService;
    }

    @ApiOperation("移动道具")
    @ApiParam(name = "propId", value = "道具ID", required = true)
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    public JsonResult<Void> move(
            @RequestParam(required = false) String repositoryId,
            @NotBlank(message = "道具" + CustomMessages.ID_NOT_NULL) String propId) {
        propService.move(propId, repositoryId);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("添加道具")
    @ApiOperationSupport(ignoreParameters = "id")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated PropDTO propDTO) {
        propService.insert(propDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID删除道具")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "道具ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        propService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改道具信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated PropDTO propDTO) {
        propService.updateById(propDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID查询道具信息")
    @RequestMapping(value = "/selectProp", method = RequestMethod.GET)
    public JsonResult<PropVO> selectProp(
            @ApiParam(name = "propId", value = "道具ID", required = true)
            @NotBlank(message = "道具" + CustomMessages.ID_NOT_NULL) String propId) {
        PropVO prop = propService.selectProp(propId);

        return JsonResult.success(null, prop);
    }

    @ApiOperation("分页查询道具列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<Prop>> selectByPage(PropPager pager) {
        Page<Prop> page = propService.selectByPage(pager);

        return JsonResult.restPage(page);
    }

}